// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('../pages/Login.vue') },
  { path: '/home', component: () => import('../pages/Home.vue') },
  { path: '/exam', component: () => import('../pages/Exam.vue') },
  { path: '/result', component: () => import('../pages/Result.vue') },
  { path: '/admin', component: () => import('../pages/admin.vue') },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局路由守卫：未登录禁止访问 /exam 和 /result
router.beforeEach((to, from, next) => {
  const employeeId = localStorage.getItem('employeeId')
  if ((to.path === '/exam' || to.path === '/result') && !employeeId) {
    next('/login')
  } else {
    next()
  }
})

export default router
